#

import re
import base64
import json

# 编码后的 ssr
ssr = "ssr://NjQuMTM3LjIwMS4yNDY6Mjk4NzphdXRoX3NoYTFfdjQ6Y2hhY2hhMjA6dGxzMS4yX3RpY2tldF9hdXRoOlpHOTFZaTVwYnk5emMzcG9abmd2S21SdmRXSXVZbWxrTDNOemVtaG1lQzhxTWprNE53Lz9yZW1hcmtzPTVweXM2TFNtNVktMzVwMmw2SWVxT21SdmRXSXVhVzh2YzNONmFHWjRMLW1Wbk9XRGotV2ZuLVdRalRwa2IzVmlMbUpwWkM5emMzcG9abmd2"


def do_decode(ssr):
    ssr = base64.urlsafe_b64decode(ssr[6:]).decode()
    ssr = re.split('[:/?]', ssr)
    "ssr://server:port:protocol:method:obfs:password_base64/?params_base64"
    ori = {
        "server": ssr[0],
        "port": ssr[1],
        "protocol": ssr[2],
        "method": ssr[3],
        "obfs": ssr[4]
    }

    padding = '=' * (len(ssr[5]) % 4)
    password = base64.urlsafe_b64decode(ssr[5] + padding).decode()
    ori['password'] = password

    # "obfsparam=obfsparam_base64&protoparam=protoparam_base64&remarks=remarks_base64&group=group_base64"

    params = ssr[7].split('&')
    ori_params = {}
    for param in params:
        k, v = param.split('=')
        padding = '=' * (len(v) % 4)
        ori_params[k] = base64.urlsafe_b64decode(v + padding).decode()
    ori['params'] = ori_params
    return ori


ssr = do_decode(ssr)
print(json.dumps(ssr, indent=2))
